package file;

import lombok.Data;

import java.io.*;
import java.util.*;

@Data
public class FileUtil implements FileHandler{

    private List<File> fileList;

    public FileUtil(){
        this.fileList = new ArrayList<>();
    }

    public static void main(String[] args) throws Exception {
        FileUtil fileUtil = new FileUtil();
        String originProj = "H:\\project-2020\\app\\new_branch\\src\\main\\java\\com\\yqjr\\car\\dealer\\";
        String destinProj = "H:\\project-2020\\app\\PROD\\src\\main\\java\\com\\yqjr\\car\\dealer\\";
        String oriprefix = "java",desprefix = "java";
        Map init =fileUtil.init(originProj, oriprefix, destinProj, desprefix);
        fileUtil.moveFile(init);
    }

    public Map init(String orginProj,String oriprefix, String destinProj, String desprefix){
        Map fileMap = new HashMap();
        Map duplicaMap = new HashMap();
        List<File> duplicaList = new ArrayList();
        List<File> allFiles = getAllFiles(orginProj,oriprefix);
        List<File> allFiles1 = getAllFiles(destinProj,desprefix);
        for (int i = 0; i < allFiles.size(); i++) {
            File file = allFiles.get(i);
            PathInfo pathInfo = new PathInfo();
            pathInfo.setFileName(file.getName());
            pathInfo.setOriginFile(file);
            Object put = fileMap.put(file.getName(), pathInfo);
            if(put != null){
                boolean flag = false;
                if(duplicaMap.size() == 0){
                    duplicaList.add(file);
                    duplicaList.add(((PathInfo) put).getOriginFile());
                    duplicaMap.put(file.getName(), duplicaList);
                }else {
                    List<File> o = duplicaMap.get(file.getName()) == null ? new ArrayList<File>() :(List<File>) duplicaMap.get(file.getName());
                    o.add(file);

                    o.add(((PathInfo) put).getOriginFile());
                    duplicaMap.put(file.getName(), o);

                }

            }

        }
        for (int i = 0; i < allFiles1.size(); i++) {
            try{
                File file = allFiles1.get(i);
                Object o = fileMap.get(file.getName());
                if(o != null){
                    PathInfo pathInfo = (PathInfo) o;
                    pathInfo.setDestFile(file);
                }else{
                    PathInfo pathInfo = new PathInfo();
                    pathInfo.setFileName(file.getName());
                    pathInfo.setDestFile(file);
                    Object put = fileMap.put(file.getName(), pathInfo);
                    if(put != null){
                        boolean flag = false;
                        for (File file1 :duplicaList) {
                            flag = file1.getAbsolutePath()!=((PathInfo)put).getOriginFile().getAbsolutePath();
                        }
                        if(duplicaMap.get(file.getName()) == null && duplicaList != null && duplicaList.size() > 0) {
                            duplicaList = new ArrayList<>();
                        }
                        if (flag) {
                            duplicaList.add(((PathInfo)put).getOriginFile());
                        }
                        duplicaList.add(file);
                        duplicaMap.put(file.getName(), duplicaList);
                        fileMap.remove(file.getName());
                    }

                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }



        System.out.println(allFiles.size() == allFiles1.size());
        return fileMap;
    }

    @Override
    public void moveFile(String origin, String destination) throws IOException {
        File originFile = new File(origin);
        File destinationDir = new File(destination.replaceAll(originFile.getName(),""));
        File destinationFile = new File(destination);
        if (!destinationDir.exists()){
            destinationDir.mkdirs();
        }
        if(!destinationFile.exists()){
            destinationFile.createNewFile();
        }
        FileInputStream fileInputStream = new FileInputStream(originFile);
        FileOutputStream fileOutputStream = new FileOutputStream(destinationFile);
        int flag = -1;
        while((flag = fileInputStream.read()) != -1){
            fileOutputStream.write(flag);
        }
        fileInputStream.close();
        fileOutputStream.close();

    }

    public void moveFile(Map fileMap) throws IOException {
        Set keys = fileMap.keySet();
        Iterator iterator = keys.iterator();
        while(iterator.hasNext()){
            Object next = iterator.next();
            PathInfo o = (PathInfo)fileMap.get(next);
            File originFile = o.getOriginFile();
            File destFile = o.getDestFile();
            //当既有读入的字节流又有写出的字节流时：java会先对写入的文件内容进行清空，在读文件
            //分别在两个线程中读和写就可以？
            if(originFile!=null && destFile != null && !originFile.getAbsolutePath().equals(destFile.getAbsolutePath())){
                moveFile(originFile.getAbsolutePath(), destFile.getAbsolutePath().replaceAll("PROD", "new_branch"));
            }
        }

    }
    public List<File> getAllFiles(String dir, String prefix) {
        return initGetAllFiles(dir, prefix, true);
    }

    public List<File> getAllFiles(String dir){
        return initGetAllFiles(dir,null,false);
    }
    public List<File> initGetAllFiles(String dir, String prefix, Boolean isFilter){
        this.fileList = new ArrayList<>();
        return getAllFiles(dir, prefix, isFilter);
    }

    @Override
    public List<File> getAllFiles(String dir, String prefix, Boolean isFilter) {
        File files = new File(dir);
        File[] listFiles = files.listFiles();
        for (File file:listFiles) {
            if (file.isFile() && (!isFilter || file.getName().endsWith("."+prefix))){
                fileList.add(file);
            }
            if (file.isDirectory()){
                getAllFiles(file.getAbsolutePath(), prefix, isFilter);
            }
        }
        return this.fileList;
    }
}
